package com.googlecode.bip.xml.export;

import java.sql.SQLException;
import java.util.List;
import javax.naming.NamingException;

/**
 * Handle XML export from MySQL database
 */
//not public
class XMLQuestionLocalFactory
    extends XMLSurveyEntryFactoryBuilder<XMLQuestionLocal>
{
    //private static final long serialVersionUID = 1L;

    protected XMLQuestionLocalFactory(
            XMLSurveyConnection connection,
            String              sql
            )
    {
        super( connection, sql );
    }

    public static List<XMLQuestionLocal> createList(
            XMLSurveyConnection connection,
            XMLQuestion         xmlQuestion
            )
        throws SQLException, NamingException
    {
        StringBuilder sql = new StringBuilder();
        sql.append( "SELECT * FROM `questions_local` WHERE `idquestion`=" )
           .append( Integer.toString( xmlQuestion.getIdQuestion() ) )
           .append( ';' );
        XMLQuestionLocalFactory xml = new XMLQuestionLocalFactory( connection, sql.toString() );
        //ResultSet rset = xml.execute();
        XMLSurveyConnection.ResultSetCache rset = xml.execute(
                "idlocal",
                "questionname",
                "questiondescription"
                );

        try {
            while( rset.next() ) {
                String idlocal              = rset.getString( "idlocal" );
                String questionname         = rset.getString( "questionname" );
                String questiondescription  = rset.getString( "questiondescription" );

                xml.add( new XMLQuestionLocal( connection, idlocal, questionname, questiondescription, xmlQuestion ) );
                }
            }
        finally {
            rset.close();
            }

        return xml.getList();
    }

}
